package com.newbie.cellsms.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.newbie.cellsms.base.Constants;
import com.newbie.cellsms.base.SQLiteUtil;
import com.newbie.cellsms.bean.GsmRadioSetting;
import com.newbie.cellsms.bean.Message;

/**
 * 用来和sqlite数据库交互的
 * 
 * @author Jacarri
 * 
 */
public class DbService {
	private static String getUpdateClause(String tableName, String ColumnValue, String columnName) {
		StringBuffer sb = new StringBuffer();
		sb.append(" update ");
		sb.append(tableName);
		sb.append("       set VALUESTRING ='");
		sb.append(ColumnValue);
		sb.append("'              where KEYSTRING ='");
		sb.append(columnName);
		sb.append("'          ");
		System.out.println(sb.toString());
		return sb.toString();
	}

	public static void settingConfirm(GsmRadioSetting grs) throws ClassNotFoundException, SQLException {
		Connection conn = SQLiteUtil.getOpenBTSConnection();
		Statement statement = conn.createStatement();
		statement.setQueryTimeout(30);
		// conn.setAutoCommit(false);
		statement.executeUpdate(DbService.getUpdateClause("config", grs.getArfcn(), Constants.KEY_ARFCN));
		statement.executeUpdate(DbService.getUpdateClause("config", grs.getMsg(), Constants.KEY_MSG));
		statement.executeUpdate(DbService.getUpdateClause("config", grs.getSrc(), Constants.KEY_SRC));
		// conn.commit();
		statement.close();
		conn.close();
	}

	public static Integer sendingShow() throws ClassNotFoundException, SQLException {
		Connection conn = SQLiteUtil.getSmqBackupConnection();
		Statement statement = conn.createStatement();
		statement.setQueryTimeout(30);
		// ResultSet rs =
		// statement.executeQuery("select message,timestamp from messages");
		ResultSet rs = statement.executeQuery("select count(*) as count  from messages");
//		List<Message> messageList = new ArrayList<Message>();
//		while (rs.next()) {
//			messageList.add(new Message(rs.getString("count"), rs.getString("timestamp")));
//		}
		return rs.getInt("count");
	}

	public static void sendResultShow() {
		// TODO Auto-generated method stub

	}

	public static void superSetting(String columnName, String columnValue) throws ClassNotFoundException, SQLException {
		Connection conn = SQLiteUtil.getOpenBTSConnection();
		Statement statement = conn.createStatement();
		statement.setQueryTimeout(30);
		// conn.setAutoCommit(false);
		statement.executeUpdate(DbService.getUpdateClause("config", columnValue, columnName));
		// conn.commit();
		statement.close();
		conn.close();
	}

}
